package io.rong.app.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yunshipei.EnterApplication;
import com.yunshipei.adapter.MyListViewAdapter;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.HeadName;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import io.rong.app.utils.StringUtilSub;

/**
 * Created by cike on 16/2/1.
 */
public class ImageAdapter extends android.widget.BaseAdapter {
    private Context mContext;
    private int dataSize;
    private List<HeadName> data;
    private ViewHolder viewHolder = null;
    private boolean state = true;
    private boolean removeState = false;
    private LruCache<String, BitmapDrawable> mMemoryCache;

    /**
     * 添加 "+" "-"
     * @param states 表示是否为群主
     */
    private void populateData(boolean states) { // 计算
        data.add(new HeadName("", "", "+"));// add "+"
        if (states)
            data.add(new HeadName("", "", "-"));// add "-"
    }

    /**
     * 删除数据
     * @param position
     */
    public void remove(int position) {
        data.remove(position);
        dataSize--;
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void add(List<HeadName> list) {
        int size = data.size();
        data.remove(size - 1);
        if (state)
            data.remove(size - 2);
        for (HeadName hN : list) {
            data.add(hN);
            dataSize++;
        }
        data.add(new HeadName("", "", "+"));
        if (state)
            data.add(new HeadName("", "", "-"));
        this.notifyDataSetChanged();
    }

    public void clear() {
        data.clear();
        dataSize = 0;
        populateData(getState());
    }

    public int getSize() {
        return dataSize;
    }

    public ImageAdapter(Context c, List<HeadName> data, boolean states) {
        mContext = c;
        this.state = states;
        this.dataSize = data.size();
        this.data = data;
        populateData(state);
    }

    public List<HeadName> getData() {
        return data;
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            viewHolder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.manage_grid_item, null);
            viewHolder.avatarView = (ImageView) convertView.findViewById(R.id.grid_item_image);
            viewHolder.nameView = (TextView) convertView.findViewById(R.id.grid_item_label);
            viewHolder.deleteView = (View) convertView.findViewById(R.id.deleteLayout);
            viewHolder.avatorName = (TextView) convertView.findViewById(R.id.avatar_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HeadName item = data.get(position);
        viewHolder.avatarView.setAdjustViewBounds(false);
        viewHolder.avatarView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        viewHolder.avatarView.setImageResource(R.drawable.de_default_portrait);
        String key1 = item.getAvatarUri();
        if (!"notimage".equals(key1) && key1 != null && position < dataSize) {
            viewHolder.avatorName.setVisibility(View.INVISIBLE);
            viewHolder.avatarView.setTag(item.getAvatarUri());
            Glide.with((Activity) mContext).load(item.getAvatarUri()).into(viewHolder.avatarView);
        } else if (position < dataSize) {
            viewHolder.avatorName.setVisibility(View.VISIBLE);
            viewHolder.avatarView.setImageResource(MyListViewAdapter.person_logos[Math.abs(position - 1) % MyListViewAdapter.person_logos.length]);
            if (StringUtilSub.isChinese(item.getName())) {
                viewHolder.avatorName.setText(StringUtilSub.getEndword(item.getName()));
            } else {
                viewHolder.avatorName.setText(item.getName().substring(0, 1).toString().toUpperCase());
            }
        }
        if (position < dataSize) {
            viewHolder.avatarView.setVisibility(View.VISIBLE);
            viewHolder.nameView.setVisibility(View.VISIBLE);
            viewHolder.nameView.setText(StringUtilSub.getInstance().subSting(0, item.getName(), 4));
            if (removeState) {
                // Display delete icon
                viewHolder.deleteView.setVisibility(View.VISIBLE);
                if (position == dataSize) {
                    viewHolder.deleteView.setVisibility(View.INVISIBLE);
                }
            } else {
                viewHolder.deleteView.setVisibility(View.INVISIBLE);
                convertView.setVisibility(View.VISIBLE);
            }
        } else {
            viewHolder.avatarView.setVisibility(View.VISIBLE);
            viewHolder.nameView.setVisibility(View.INVISIBLE);
            viewHolder.deleteView.setVisibility(View.INVISIBLE);
            viewHolder.avatorName.setVisibility(View.INVISIBLE);
            Bitmap addImage;
            Resources res = mContext.getResources();
            if (position == dataSize) {
                addImage = BitmapFactory.decodeResource(res, R.drawable.group_add_member);
            } else {
                addImage = BitmapFactory.decodeResource(res, R.drawable.group_remove_member);
            }
            viewHolder.avatarView.setImageBitmap(addImage);
        }
        return convertView;
    }

    /**
     * 获取删除状态
     * @return
     */
    public boolean getRemoveState() {
        return removeState;
    }

    /**
     * 设置删除状态
     * @param removeState
     */
    public void setRemoveState(boolean removeState) {
        this.removeState = removeState;
    }

    public boolean getState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

    class ViewHolder {
        private ImageView avatarView;
        private View deleteView;
        private TextView nameView, avatorName;
    }
}
